package com.example.deertrip;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import com.example.deertrip.bean.Flight;
import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.tools_class.DrawableHelp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class SearchResult_Activity extends AppCompatActivity
{
private Button btn_nolyhotel;
private Button btn_onlyflight;
private EditText input;
private ImageButton research_btn;
private ListView listView_hotel;
private ListView listView_flight;
    List<Tb_HotelInfo> hotels;
    List<Flight> flights;
    List<HashMap<String, Object>> orgdata_hotel=new ArrayList<>();
    List<HashMap<String, Object>> orgdata_flight=new ArrayList<>();
    String search;
    private DBOperate dbOperate;
    SimpleAdapter adapter_hotel,adapter_flight;
    List<HashMap<String, Object>> data_flight = new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
    List<HashMap<String, Object>> data_hotel = new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
    Drawable dw;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchresult);

        btn_nolyhotel=findViewById(R.id.btn_onlyhotel);
        btn_onlyflight=findViewById(R.id.btn_onlyflight);
        input=findViewById(R.id.search_input);
        research_btn=findViewById(R.id.search_button);
        listView_hotel=findViewById(R.id.searchresult_hotel_lv);
        listView_flight=findViewById(R.id.searchresult_filght_lv);
        dbOperate=new DBOperate(this);
        show();
        listen();
    }

    private void listen() {
        research_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String research=input.getText().toString();
                if(Objects.equals(research, "")){
                    hotels=dbOperate.get_All_HotelInfo();
                    flights=dbOperate.get_All_Flight_Data();
                }
                else{
                    flights=dbOperate.Search_Flight(research);
                    hotels= dbOperate.Search_Hotel(research);
                }
            data_flight.clear();
                data_hotel.clear();

                for (int i = 0; i < flights.size(); i++) {
                    HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表
                    Flight flight = flights.get(i);
                    BitmapDrawable bd = new BitmapDrawable(getResources(), flight.getCompanyImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
                    dw = bd;    //再转换为Drawable
                    items.put("CompanyImage", dw);   //属性分别存入哈希表
                    items.put("FlightNumber",flight.getFlightNumber());
                    items.put("TakeoffTime", flight.getTakeoffTime());
                    items.put("LandingTime", flight.getLandingTime());
                    items.put("FlightForm", flight.getFlightForm());
                    items.put("FlightTo", flight.getFlightTo());
                    data_flight.add(items);   //总数据哈希表中加入该条数据项
                }

                for(int i=0;i<hotels.size();i++){
                    HashMap<String, Object> items=new HashMap<String, Object>(); //存放一条数据的哈希表

                    Tb_HotelInfo HotelInfo=hotels.get(i);
                    BitmapDrawable bd=new BitmapDrawable(getResources(),HotelInfo.getHotelImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
                    dw=bd;    //再转换为Drawable
                    items.put("HotelImage", dw);   //属性分别存入哈希表
                    items.put("HotelAddress", HotelInfo.getHotelAddress());
                    items.put("HotelScore", HotelInfo.getHotelScore());
                    items.put("HotelName", HotelInfo.getHotelName());
                    items.put("HotelTel", HotelInfo.getHotelTel());
                    data_hotel.add(items);   //总数据哈希表中加入该条数据项
                }

               orgdata_flight.addAll(data_flight);
                orgdata_hotel.addAll(data_hotel);



                adapter_hotel.notifyDataSetChanged();
                adapter_flight.notifyDataSetChanged();
            }
        });


        btn_nolyhotel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data_flight.clear();
                adapter_flight.notifyDataSetChanged();
                if(data_hotel.size()==0)data_hotel.addAll(orgdata_hotel);
                adapter_hotel.notifyDataSetChanged();

            }
        });


        btn_onlyflight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if(data_flight.size()==0) data_flight.addAll(orgdata_flight);
                data_hotel.clear();
                adapter_hotel.notifyDataSetChanged();
                adapter_flight.notifyDataSetChanged();

            }
        });
    }

    private void show() {
        search=getIntent().getStringExtra("search");
        if(Objects.equals(search, "")){
            hotels=dbOperate.get_All_HotelInfo();
            flights=dbOperate.get_All_Flight_Data();
        }
        else{
            flights=dbOperate.Search_Flight(search);
            hotels= dbOperate.Search_Hotel(search);
        }
        sql();
    }

    public void sql() {


        for (int i = 0; i < flights.size(); i++) {
            HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表
            Flight flight = flights.get(i);
            BitmapDrawable bd = new BitmapDrawable(this.getResources(), flight.getCompanyImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw = bd;    //再转换为Drawable
            items.put("CompanyImage", dw);   //属性分别存入哈希表
            items.put("FlightNumber",flight.getFlightNumber());
            items.put("TakeoffTime", flight.getTakeoffTime());
            items.put("LandingTime", flight.getLandingTime());
            items.put("FlightForm", flight.getFlightForm());
            items.put("FlightTo", flight.getFlightTo());
            data_flight.add(items);   //总数据哈希表中加入该条数据项
        }

        for(int i=0;i<hotels.size();i++){
            HashMap<String, Object> items=new HashMap<String, Object>(); //存放一条数据的哈希表

            Tb_HotelInfo HotelInfo=hotels.get(i);
            BitmapDrawable bd=new BitmapDrawable(this.getResources(),HotelInfo.getHotelImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw=bd;    //再转换为Drawable
            items.put("HotelImage", dw);   //属性分别存入哈希表
            items.put("HotelAddress", HotelInfo.getHotelAddress());
            items.put("HotelScore", HotelInfo.getHotelScore());
            items.put("HotelName", HotelInfo.getHotelName());
            items.put("HotelTel", HotelInfo.getHotelTel());
            data_hotel.add(items);   //总数据哈希表中加入该条数据项
        }

        orgdata_flight.addAll(data_flight);
        orgdata_hotel.addAll(data_hotel);


        adapter_hotel = new SimpleAdapter(this, data_hotel, R.layout.hotel_info_bar,
                new String[]{"HotelImage","HotelAddress", "HotelScore","HotelName","HotelTel"}, new int[]{R.id.htimage,R.id.htaddress, R.id.htscore,R.id.htname, R.id.httel});

        adapter_hotel.setViewBinder(new SimpleAdapter.ViewBinder(){
            public boolean setViewValue(View view,Object data,String textRepresentation){
                if(view instanceof ImageView && data instanceof Drawable){
                    ImageView iv=(ImageView)view;
                    iv.setImageDrawable((Drawable)data);
                    return true;
                }
                else return false;
            }
        });

       adapter_flight = new SimpleAdapter(this, data_flight, R.layout.flight_info_bar,
                new String[]{"CompanyImage", "FlightNumber", "TakeoffTime", "LandingTime", "FlightForm", "FlightTo"}, new int[]{R.id.iv_1, R.id.tv_5, R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4});

        adapter_flight.setViewBinder(new SimpleAdapter.ViewBinder() {
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view instanceof ImageView && data instanceof Drawable) {
                    ImageView iv = (ImageView) view;
                    iv.setImageDrawable((Drawable) data);
                    return true;
                } else return false;
            }
        });

        listView_flight.setAdapter(adapter_flight);
        listView_hotel.setAdapter(adapter_hotel);

        listView_hotel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> items=new HashMap<String, Object>();
                Bundle bundle=new Bundle();
                items=data_hotel.get(i);
                bundle.putString("HotelAddress",(String) items.get("HotelAddress"));
                bundle.putString("HotelScore",(String)items.get("HotelScore"));
                bundle.putString("HotelName",(String)items.get("HotelName"));
                bundle.putLong("HotelTel",(Long)items.get("HotelTel"));


                Drawable image=(Drawable) items.get("HotelImage");
                byte[] out= DrawableHelp.drawableTobyte(image);

                Intent intent=new Intent(SearchResult_Activity.this,HotelActivity.class);
                intent.putExtras(bundle);
                intent.putExtra("HotelImage",out);
                startActivity(intent);
            }
        });

        listView_flight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> items=new HashMap<String, Object>();
                Bundle bundle=new Bundle();
                items=data_flight.get(i);
                bundle.putString("TakeoffTime",(String)items.get("TakeoffTime"));
                bundle.putString("CompanyName",(String)items.get("CompanyName"));
                bundle.putString("TotalHour",(String)items.get("TotalHour"));
                bundle.putInt("TicketPrice",(Integer) items.get("TicketPrice"));
                System.out.println(items.get("TicketPrice"));
                bundle.putInt("P2",(Integer) items.get("P2"));
                bundle.putInt("P3",(Integer) items.get("P3"));
                bundle.putString("LandingTime",(String)items.get("LandingTime"));
                bundle.putString("FlightForm",(String)items.get("FlightForm"));
                bundle.putString("FlightTo",(String)items.get("FlightTo"));
                Intent intent=new Intent(SearchResult_Activity.this, Flight_Choose_Activity.class);
                intent.putExtras(bundle);

                startActivity(intent);
            }
        });

    }


    public void dothing(){
    int a;
          for(int i=9000;i>0;i--) a=i;
}
}
